package cn.edu.nwafu.hjpg.controller;


import cn.edu.nwafu.hjpg.common.HttpResult;
import cn.edu.nwafu.hjpg.entity.Hyxxb;
import cn.edu.nwafu.hjpg.entity.Lhsxb;
import cn.edu.nwafu.hjpg.service.HyxxbService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/hyxxb")
public class HyxxbController {
    final HyxxbService hyxxbService;

    public HyxxbController(HyxxbService hyxxbService) {
        this.hyxxbService = hyxxbService;
    }
    //private boolean required;


    @GetMapping(value = "/")
    @ResponseBody
    public HttpResult get(@RequestParam(value = "labelName",defaultValue = "",required = false) String name,
                          @RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                          @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        System.out.println(flag);
        return HttpResult.ok(hyxxbService.findByIdAndFlagInHyxxb(id,name,flag));
    }
    @PostMapping(value = "/update")
    @ResponseBody
    public HttpResult update(@RequestBody Hyxxb hyxxb) {
        return HttpResult.ok(hyxxbService.updatehyxxb(hyxxb));
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public HttpResult add(@RequestBody(required = false) Hyxxb hyxxb,@RequestParam(value = "labelName",defaultValue = "",required = false) String name) {
        switch(name){
            case "主要用途描述":
                return HttpResult.ok(hyxxbService.addZyytms(hyxxb));
            case "行业生产量":
                return HttpResult.ok(hyxxbService.addHyscl(hyxxb));
            case "行业使用量":
                return HttpResult.ok(hyxxbService.addHysyl(hyxxb));
            case "行业功能（多个）":
                return HttpResult.ok(hyxxbService.addHygndg(hyxxb));
            case "行业应用领域":
                return HttpResult.ok(hyxxbService.addHyyyly(hyxxb));
            case "行业生产工艺描述":
                return HttpResult.ok(hyxxbService.addHyscgyms(hyxxb));
            case "行业使用工艺描述":
                return HttpResult.ok(hyxxbService.addHysygyms(hyxxb));
            case "生产使用备注":
                return HttpResult.ok(hyxxbService.addScsybz(hyxxb));
            case "排放系数（大气）":
                return HttpResult.ok(hyxxbService.addPfxsdq(hyxxb));
            case "排放系数（水）":
                return HttpResult.ok(hyxxbService.addPfxss(hyxxb));
            default:
                return null;
        }

    }
    @GetMapping(value = "/export")
    @ResponseBody
    public HttpResult get() {
        return HttpResult.ok(hyxxbService.findAll());
    }

    @GetMapping(value = "/addRecord")
    @ResponseBody
    public HttpResult queryHyxxbByPage(@RequestParam(value = "flag", defaultValue = "") String flag,
                                      @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "pageSize", defaultValue = "20") int pageSize){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(hyxxbService.findAddRecordHyxxb(flag,pageable));
    }

    @GetMapping(value = "/find")
    @ResponseBody
    public HttpResult findHyxxbByPage(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "pageSize", defaultValue = "7") int pageSize,
                                      @RequestParam(value = "attribute", defaultValue = "") String attribute,
                                      @RequestParam(value = "flag", defaultValue = "1") int flag,
                                      @RequestParam(value = "id",defaultValue = "",required = false) String id){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(hyxxbService.findByPageHyxxb(id,attribute,flag,pageable));
    }

    @PostMapping(value = "/addupdateFlag")
    @ResponseBody
    public HttpResult addupdateFlag(@RequestBody Hyxxb hyxxb) {
        System.out.println(hyxxb.toString());
        return HttpResult.ok(hyxxbService.addupdateFlag(hyxxb));
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public HttpResult deladdRecordhyxxb(Hyxxb hyxxb){
        hyxxbService.deladdRecordhyxxb(hyxxb);
        return HttpResult.ok();
    }
    @PostMapping(value = "/import")
    @ResponseBody
    public HttpResult addExcelInHyxxb(@RequestBody List<Hyxxb> hyxxbList) {
        for (int i = 0 ; i <hyxxbList.size() ; i ++ ){
            hyxxbList.get(i).setFlag("1");
            hyxxbService.addElement(hyxxbList.get(i));
        }
        return HttpResult.ok();
    }
    @GetMapping(value = "/all")
    @ResponseBody
    public HttpResult findAll(@RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                              @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        System.out.println("**********************");
        return HttpResult.ok(hyxxbService.findAllByBaseinfoWzbmAndFlag(id,flag));
    }
}
